package com.example.index.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.index.service.IFolderService;
import com.yc.biz.BizException;
import com.example.index.mapper.FolderMapper;
import com.yc.pojo.Collect;
import com.yc.pojo.Folder;
import com.yc.util.Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxh
 * @since 2023-07-07
 */
@Service
public class FolderServiceImpl extends ServiceImpl<FolderMapper, Folder> implements IFolderService {

    @Resource
    FolderMapper folderMapper;

    @Resource
    CollectServiceImpl collectService;

    public void addFolder(String folderName, Integer userId) throws BizException {
        Utils.checkEmpty(folderName, "文件夹名不能为空!");
        Folder folder = new Folder();
        folder.setFolderName(folderName);
        folder.setUserId(userId);
        folderMapper.insert(folder);
    }

    public void deleteFolder(String folderIds) throws BizException {
        Utils.checkEmpty(folderIds,"未选择收藏夹!");
        final String[] split = folderIds.split(",");
        for (String folderId : split) {
            LambdaUpdateWrapper<Collect> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.set(Collect::getFolderId, 0).eq(Collect::getFolderId, folderId);
            collectService.update(lambdaUpdateWrapper);
            folderMapper.deleteById(folderId);
        }
    }

    public void deleteFolder(Integer folderId) throws BizException {
        Utils.checkEmpty(folderId, "系统繁忙，请稍后重试!");
        LambdaUpdateWrapper<Collect> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(Collect::getFolderId, 0).eq(Collect::getFolderId, folderId);
        collectService.update(lambdaUpdateWrapper);
        folderMapper.deleteById(folderId);
    }

    public void modFolderName(Integer folderId, String folderName) throws BizException {
        Utils.checkEmpty(folderName, "收藏夹名不能为空！");
        Utils.checkEmpty(folderId, "系统繁忙，请稍后重试！");
        LambdaUpdateWrapper<Folder> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(Folder::getFolderName,folderName).eq(Folder::getId, folderId);
        this.update(lambdaUpdateWrapper);
    }

    public void updateArticleByFolder(String articleIds, Integer folderId, Integer userId) throws BizException {
        Utils.checkEmpty(articleIds, "未选择需添加的收藏房源!");
        Utils.checkEmpty(userId, "系统繁忙，请稍后重试!");
        final String[] split = articleIds.split(",");
        for (String articleId : split) {
            LambdaUpdateWrapper<Collect> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            lambdaUpdateWrapper.set(Collect::getFolderId, folderId)
                            .eq(Collect::getUserId, userId)
                            .eq(Collect::getHouseId, articleId);
            collectService.update(lambdaUpdateWrapper);
        }

    }

}
